var basePath = "/";
var parentParams = artDialog.data('params');
var map;
var point;
var starMap;
var cate;
function sendData(params){
  cate = params.cate;
  map = params.map;
  point = params.result;
  starMap = window.parent.starMap;
  $("#addr").text(point.getAddress());
  $("#lng").text(point.getLocationPoint().lng);
  $("#lat").text(point.getLocationPoint().lat);
}
$(function() {
  $("#editWarInfoForm").validate({
    rules : {
      cateId : {
        required : true
      },
      lng : {
        required : true,
        zhengze : ".{2,20}"
      },
      lat : {
        required : true,
        zhengze : ".{2,20}"
      },
      stationNames:{
        zhengze: "\\s*[A-Za-z0-9_\u4e00-\u9fa5]+\\s*([,，]\\s*[A-Za-z0-9_\u4e00-\u9fa5]+\\s*)*"
      }
    },
    messages : {
      cateId : {
        required : "必选"
      },
      lng : {
        required : "必填",
        zhengze : "长度在2至20个字"
      },
      lat : {
        required : "必填",
        zhengze : "长度在2至20个字"
      },
      stationNames:{
        zhengze: "由汉字字母数字下划线组成，多个用逗号分隔"
      }
    }
  });
  var handle = new UtilsHandle({
    basePath: "/",
    uploadImages:{uploadFileId: 'uploadImage', multiple: true, items: [{
      data: {"mark": 0, "of": 1},
      uploadBtn: $('#uploadImgBtn'), 
      success: function (data, textStatus) {
        if(data.code==0){
          for(var o in data.data){
            var pic = data.data[o].original;
            $('#imgSpan').append('<span style="position: relative;"><img class="dataImg" width="70px" height="70px" src="'+pic+'" data="'+pic+'"><div class="close">X</div></span>'); 
          }
        }else{
          artDialog.alert(data.msg);
        }
      },
      complete: function (XMLHttpRequest, textStatus) {
        $('.close').unbind().click(function(){
          $(this).parent().remove();
        }); 
        $('.dataImg').unbind().click(function(){
          if($('#viewImg').length>0){ 
            $('#viewImg').remove(); 
          } 
          $('body').append('<img id="viewImg" style="display:none" src="'+$(this).attr("src")+'">');
          $('#viewImg').on("load", function(){
            var w=$(this).width();
            var h=$(this).height();
            var l = w / h;
            if(h > handle.getScreenHeight() * 0.85){
              h = handle.getScreenHeight() * 0.85;
              w = h * l;
            }
            artDialog.alert2('<div style="width:'+w+'px;height:'+h+'px;"><img style="height:'+h+'px" src="'+$(this).attr("src")+'">') 
          }); 
        }); 
      } 
    }]
    }
  });
  
  $("#saveBtn").click(function() {
    var flag = $("#editWarInfoForm").valid();
    if (!flag)
      return;
    var data = $("#editWarInfoForm").serializeArray();
    var params = {};
    $.each(data, function(i, field) {
      var name = field.name;
      params[name] = field.value;
      console.log(name + "  " + field.value)
    });
    params['lng'] = point.getLocationPoint().lng;
    params['lat'] = point.getLocationPoint().lat;
    params['cateId'] = cate.cateId;
    var pictures = new Array();
    $("#imgSpan .dataImg").each(function(){
      var path = $(this).attr("data");
      if(path) {
        pictures.push({"url": path});
      }
    });
    params["pictures"] = pictures;
    var infos = {};
    $("tr.infoTr").each(function(){
      var key = $.trim($("strong span", $(this)).text());
      var val = $("input", $(this)).val();
      infos[key] = val;
    });
    params["infos"] = infos;
    if(params.stationNames){
      var stations = params.stationNames.split(/[,，]/);
      var nf = [];
      for(var o in stations){
        var f = stations[o].replace(/\s*/g, "");
        if(nf.indexOf(f) != -1){
          artDialog.alert("监测站【"+f+"】已存在");
          return false;
        }
        nf.push(f);
      }
      params.stations = nf;
    }
    console.log(JSON.stringify(params));
    $("#saveBtn").attr("disabled", true);
    $.ajax({
      contentType : "application/json",
      url : basePath + "warInfo/add",
      data : JSON.stringify(params),
      type : 'post',
      dataType : 'json',
      complete: function(xhr, ts){
        $("#saveBtn").removeAttr("disabled");
      },
      success : function(res) {
        if (res.code == 0) {
          params['id']=res.data;
          starMap.addMarker(params);
          starMap.bindMarkerInfoWin();
          null != starMap ? starMap.getMap().closeInfoWindow() : art.dialog.close();
        } else {
          artDialog.alert(res.msg)
        }
        $("#saveBtn").removeAttr("disabled");
      }
    });
  });
});